package com.ssyc.common.quanzi;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.R;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.utils.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 相册个人资料
 * Created by Administrator on 2018/7/10 0010.
 */

public class CircleRvAdapter extends BaseQuickAdapter<QuanZiInfo.CirclelistBean, BaseViewHolder> {
    private Context context;

    private List<QuanZiInfo.CirclelistBean> datas;

    public CircleRvAdapter(Context context, int layoutResId, @Nullable List<QuanZiInfo.CirclelistBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.datas = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, QuanZiInfo.CirclelistBean item) {

        TextView tvToday = helper.getView(R.id.tv_today);

        //显示日和月的布局,注意和tvToday只能显示一个
        LinearLayout llDayMonth = helper.getView(R.id.ll_day_month);
        //月和日
        TextView tvDay = helper.getView(R.id.tv_day);
        TextView tvMonth = helper.getView(R.id.tv_month);

        //时间,注意当时间是今天时,显示今天,否则显示日 月  13 6月 ,服务器返回的时间是: "update_time":"2018-07-11 09:25:25",
        //格式化时间
        //获取服务器返回时间的毫秒值
        long serverTime = DateUtils.getTime(item.getUpdate_time(), "yyyy-MM-dd HH:mm:ss");

        //将服务器返回的毫秒值转化为年月日的格式
        String serverDate = DateUtils.getTime(new Date(serverTime), "yyyy-MM-dd");

        //获取当前时间的年月日
        String currDate = DateUtils.getTime(new Date(), "yyyy-MM-dd");

        if (serverDate.equals(currDate)) {
            //表示是今天
            llDayMonth.setVisibility(View.GONE);
            tvToday.setText("今天");
            if (helper.getLayoutPosition() > 1) {
                long serverPreTime = DateUtils.getTime(datas.get(helper.getLayoutPosition() - 1).getUpdate_time(), "yyyy-MM-dd HH:mm:ss");
                //将服务器返回的毫秒值转化为年月日的格式
                String serverPreDate = DateUtils.getTime(new Date(serverPreTime), "yyyy-MM-dd");
                if (serverPreDate.equals(serverDate)) {
                    //表示当前的时间和上一个条目的时间是同一天,那么就不显示当前的时间
                    tvToday.setVisibility(View.GONE);
                } else {
                    tvToday.setVisibility(View.VISIBLE);
                }

            } else {
                //第一个位置不需要比较直接显示
                tvToday.setVisibility(View.VISIBLE);
            }

        } else {
            //不是今天
            tvToday.setVisibility(View.GONE);

            if (helper.getLayoutPosition() > 1) {
                long serverPreTime = DateUtils.getTime(datas.get(helper.getLayoutPosition() - 1).getUpdate_time(), "yyyy-MM-dd HH:mm:ss");
                //将服务器返回的毫秒值转化为年月日的格式
                String serverPreDate = DateUtils.getTime(new Date(serverPreTime), "yyyy-MM-dd");
                if (serverPreDate.equals(serverDate)) {
                    //表示当前的时间和上一个条目的时间是同一天,那么就不显示当前的时间
                    llDayMonth.setVisibility(View.GONE);
                    tvDay.setText(getDay(serverDate));
                    tvMonth.setText(getMonth(serverDate) + "月");
                } else {
                    llDayMonth.setVisibility(View.VISIBLE);
                    tvDay.setText(getDay(serverDate));
                    tvMonth.setText(getMonth(serverDate) + "月");
                }
            } else {
                //第一个位置不需要比较直接显示
                llDayMonth.setVisibility(View.VISIBLE);
                tvDay.setText(getDay(serverDate));
                tvMonth.setText(getMonth(serverDate) + "月");
            }
        }


        //图层,分别包含四层,根据服务器返回的图片数量来显示,当返回的图片数量大于等于4时,显示4张图片,当等于3时,显示三张图片
        //当等于2时,显示2张图片,当等于1时,显示一张图片,注意,四个图层只能显示一个
        RelativeLayout rlPicOut = helper.getView(R.id.rl_pic_out);

        RelativeLayout rlFour = helper.getView(R.id.rl_four);
        ImageView ivFour1 = helper.getView(R.id.iv_four1);
        ImageView ivFour2 = helper.getView(R.id.iv_four2);
        ImageView ivFour3 = helper.getView(R.id.iv_four3);
        ImageView ivFour4 = helper.getView(R.id.iv_four4);

        RelativeLayout rlThree = helper.getView(R.id.rl_three);
        ImageView ivThree1 = helper.getView(R.id.iv_three1);
        ImageView ivThree2 = helper.getView(R.id.iv_three2);
        ImageView ivThree3 = helper.getView(R.id.iv_three3);

        RelativeLayout rlTwo = helper.getView(R.id.rl_two);
        ImageView ivTwo1 = helper.getView(R.id.iv_two1);
        ImageView ivTwo2 = helper.getView(R.id.iv_two2);

        RelativeLayout rlOne = helper.getView(R.id.rl_one);
        ImageView ivOne1 = helper.getView(R.id.iv_one1);

        List<String> picUrls = new ArrayList<>();


        if (!TextUtils.isEmpty(item.getS_user_picture1())) {
            picUrls.add(item.getS_user_picture1());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture2())) {
            picUrls.add(item.getS_user_picture2());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture3())) {
            picUrls.add(item.getS_user_picture3());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture4())) {
            picUrls.add(item.getS_user_picture4());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture5())) {
            picUrls.add(item.getS_user_picture5());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture6())) {
            picUrls.add(item.getS_user_picture6());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture7())) {
            picUrls.add(item.getS_user_picture7());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture8())) {
            picUrls.add(item.getS_user_picture8());
        }
        if (!TextUtils.isEmpty(item.getS_user_picture9())) {
            picUrls.add(item.getS_user_picture9());
        }


        if (picUrls.size() >= 4) {
            rlPicOut.setVisibility(View.VISIBLE);
            rlFour.setVisibility(View.VISIBLE);
            rlThree.setVisibility(View.GONE);
            rlTwo.setVisibility(View.GONE);
            rlOne.setVisibility(View.GONE);
            ImageUtil.displayImage(context, ivFour1, picUrls.get(0), R.drawable.defaultimage);
            ImageUtil.displayImage(context, ivFour2, picUrls.get(1), R.drawable.defaultimage);
            ImageUtil.displayImage(context, ivFour3, picUrls.get(2), R.drawable.defaultimage);
            ImageUtil.displayImage(context, ivFour4, picUrls.get(3), R.drawable.defaultimage);
        } else if (picUrls.size() == 3) {
            rlPicOut.setVisibility(View.VISIBLE);
            rlFour.setVisibility(View.GONE);
            rlThree.setVisibility(View.VISIBLE);
            rlTwo.setVisibility(View.GONE);
            rlOne.setVisibility(View.GONE);
            ImageUtil.displayImage(context, ivThree1, picUrls.get(0), R.drawable.defaultimage);
            ImageUtil.displayImage(context, ivThree2, picUrls.get(1), R.drawable.defaultimage);
            ImageUtil.displayImage(context, ivThree3, picUrls.get(2), R.drawable.defaultimage);
        } else if (picUrls.size() == 2) {
            rlPicOut.setVisibility(View.VISIBLE);
            rlFour.setVisibility(View.GONE);
            rlThree.setVisibility(View.GONE);
            rlTwo.setVisibility(View.VISIBLE);
            rlOne.setVisibility(View.GONE);
            ImageUtil.displayImage(context, ivTwo1, picUrls.get(0), R.drawable.defaultimage);
            ImageUtil.displayImage(context, ivTwo2, picUrls.get(1), R.drawable.defaultimage);
        } else if (picUrls.size() == 1) {
            rlPicOut.setVisibility(View.VISIBLE);
            rlFour.setVisibility(View.GONE);
            rlThree.setVisibility(View.GONE);
            rlTwo.setVisibility(View.GONE);
            rlOne.setVisibility(View.VISIBLE);
            ImageUtil.displayImage(context, ivOne1, picUrls.get(0), R.drawable.defaultimage);
        } else {
            rlPicOut.setVisibility(View.GONE);
            rlFour.setVisibility(View.GONE);
            rlThree.setVisibility(View.GONE);
            rlTwo.setVisibility(View.GONE);
            rlOne.setVisibility(View.GONE);
        }

        //发布的评论内容
        TextView tvContent = helper.getView(R.id.tv_content);
        if (TextUtils.isEmpty(item.getContent())) {
            tvContent.setVisibility(View.GONE);
        } else {
            tvContent.setVisibility(View.VISIBLE);
            tvContent.setText(item.getContent());
        }

        //显示图片的张图
        TextView tvNun = helper.getView(R.id.tv_num);
        if (picUrls.size() == 0) {
            tvNun.setVisibility(View.GONE);
        } else {
            tvNun.setVisibility(View.VISIBLE);
            tvNun.setText("共" + picUrls.size() + "张");

        }
    }

    /**
     * 获取服务器返回的天
     * 2018-07-11
     *
     * @return
     */
    private String getDay(String date) {
        String res = "";
        if (!TextUtils.isEmpty(date)) {
            String[] split = date.split("-");
            res = split[2];
        }
        return res;
    }


    /**
     * 获取服务器返回的月
     *
     * @return
     */
    private String getMonth(String date) {
        String res = "";
        if (!TextUtils.isEmpty(date)) {
            String[] split = date.split("-");
            res = split[1];
        }
        return res;
    }

}


